package com.simon.springbootdemo.controller;

import cn.hutool.core.util.IdUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.iteaj.iot.plc.siemens.SiemensModel;
import com.simon.springbootdemo.bean.User;
import com.simon.springbootdemo.iot.collect.action.SiemensCollectAction;
import com.simon.springbootdemo.iot.consts.IotConsts;
import com.simon.springbootdemo.iot.control.DebugCmd;
import com.simon.springbootdemo.iot.control.plc.PlcDebugHandle;
import com.simon.springbootdemo.iot.dto.CollectTaskDto;
import com.simon.springbootdemo.iot.dto.DeviceDto;
import com.simon.springbootdemo.iot.entity.*;
import com.simon.springbootdemo.iot.result.HttpResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import static com.simon.springbootdemo.iot.consts.FieldTypeConsts.*;
import static com.simon.springbootdemo.iot.consts.IotConsts.FIELD_TYPE_INT;
import static com.simon.springbootdemo.iot.consts.IotConsts.FIELD_TYPE_SHORT;

@RestController
@RequestMapping("iot")
@Slf4j
public class IotController {


    @Autowired
    private SiemensCollectAction siemensCollectAction;


    @Autowired
    private PlcDebugHandle plcDebugHandle;

    @GetMapping("/read")
    public ResponseEntity<Void> read() {
        CollectTaskDto task = new CollectTaskDto();
        // User simon = new User("simon", 30);
        // return new ResponseEntity(simon, HttpStatus.OK);
        CollectDetail detail = new CollectDetail();
        DeviceDto device = new DeviceDto();
        device.setIp("127.0.0.1");
        device.setPort(103);
        device.setModelName("S200Smart");
        detail.setDevice(device);
        Signal signal = new Signal();
        signal.setAddress("VW768");
        signal.setFieldType(FIELD_TYPE_INT);
        siemensCollectAction.doExec(task, detail, signal, System.out::println);
        return new ResponseEntity<Void>(HttpStatus.OK);
    }


    @GetMapping("/debug")
    public ResponseEntity<HttpResult> debug() {
        IotDevice device = new IotDevice();
        device.setDeviceIp("127.0.0.1");
        device.setDevicePort(103);
        device.setDeviceCategory(IotConsts.COLLECT_ACTION_PLC_SIEMENS);
        device.setDeviceModel(SiemensModel.S200Smart.name());
        device.setDeviceSn(IdUtil.fastSimpleUUID());
        log.info("device:{}", JSON.toJSONString(device));
        IotPoint point = new IotPoint();
        point.setPointName("测试点位");
        point.setPointAddress("VW768");
        point.setFieldType(KEY_FIELD_TYPE_INT);
        log.info("point:{}", JSON.toJSONString(point));
        HttpResult result = plcDebugHandle.debug(device, point, DebugCmd.READ);
        // log.info("result:{}", JSONObject.toJSONString(result));
        System.out.println();
        point.setPointValue("188");
        result = plcDebugHandle.debug(device, point, DebugCmd.WRITE);
        // log.info("result:{}", JSONObject.toJSONString(result));
        return new ResponseEntity<HttpResult>(result, HttpStatus.OK);
    }



}
